package com.macro.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.common.exception.Asserts;
import com.macro.mall.mapper.CurCourseStatMapper;
import com.macro.mall.mapper.CurVideoMapper;
import com.macro.mall.model.CurCourseVideoRel;
import com.macro.mall.model.CurVideo;
import com.macro.mall.service.CurCourseVideoRelService;
import com.macro.mall.service.CurVideoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
/**
 * CurVideo的服务接口的实现类
 * 
 * @author gaodx
 *
 */
@Slf4j
@Service
public class CurVideoServiceImpl implements CurVideoService {


    @Value("${log.isDebugEnabled:false}")
    private boolean isDebugEnabled;

	@Autowired
	private CurVideoMapper curVideoMapper;
	@Autowired
	private CurCourseVideoRelService curCourseVideoRelService;

	@Autowired
	private CurCourseStatMapper curCourseStatMapper;


	@Override
	public List<CurVideo> find(Integer sourceId, Integer pageNum, Integer pageSize){
		//TODO这里可以做通过Assist做添加查询
        PageHelper.startPage(pageNum, pageSize);
		List<CurVideo> result = curVideoMapper.selectCurVideoBySourceId(sourceId);
		if (isDebugEnabled) {
			log.debug("执行获取CurVideo数据集-->结果:", result);
		}
		return result;
	}
	@Override
	public CurVideo findOne(Long id) {
		if (id == null) {
			if (isDebugEnabled) {
				log.debug("执行通过CurVideo的id获得CurVideo对象-->失败:id不能为空");
			}
            Asserts.fail("执行通过CurVideo的id获得CurVideo对象-->失败:id不能为空");
		}
		CurVideo result = curVideoMapper.selectCurVideoById(id);
		if (isDebugEnabled) {
			log.debug("执行通过CurVideo的id获得CurVideo对象-->结果:", result);
		}
		return result;
	}

	@Override
	public CommonResult saveNotNull(CurVideo value) {
		if (value == null) {
			if (isDebugEnabled) {
				log.debug("执行将CurVideo中属性值不为null的数据保存到数据库-->失败:对象不能为空");
			}
                Asserts.fail("失败:对象不能为空");
		}
		int result = curVideoMapper.insertNotNullCurVideo(value);
        curCourseVideoRelService.saveNotNull(CurCourseVideoRel.builder().courseId(value.getCourseId()).videoId(value.getId()).build());
		if (isDebugEnabled) {
			log.debug("执行将CurVideo中属性值不为null的数据保存到数据库-->结果:", result);
		}
            return CommonResult.success(null);
	}
	@Override
	public CommonResult updateNotNullById(CurVideo value) {
		if (value == null) {
			if (isDebugEnabled) {
				log.debug("执行通过CurVideo的id更新CurVideo中属性不为null的数据-->失败:对象为null");
			}
                Asserts.fail("执行通过CurVideo的id更新CurVideo中属性不为null的数据-->失败:对象为null");
		}
		int result = curVideoMapper.updateNotNullCurVideoById(value);
		if (isDebugEnabled) {
			log.debug("执行通过CurVideo的id更新CurVideo中属性不为null的数据-->结果:", result);
		}
        return CommonResult.success(null);
	}

	@Override
    @Transactional
	public CommonResult deleteById(Long id, Long courseId) {
		if (id == null) {
			if (isDebugEnabled) {
				log.debug("执行通过CurVideo的id删除CurVideo-->失败:id不能为空");
			}
                Asserts.fail("执行通过CurVideo的id删除CurVideo-->失败:id不能为空");
		}
		int result = curVideoMapper.deleteCurVideoById(id);
        curCourseVideoRelService.deleteCurCourseVideoRel(id, courseId);
        if (isDebugEnabled) {
			log.debug("执行通过CurVideo的id删除CurVideo-->结果:", result);
		}
        return CommonResult.success(null);
	}


}